var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
const app = getApp()
// 触底上拉刷新 TODO 这里要将page传给服务器，作者没写
Page({
  data: {
    // 设备机型
    isIphoneX: 0,
    // 当前显示订单列表
    orderList: [],
    // 全部订单列表
    allOrderList: [],
    // 订单页数
    allPage: 1,
    // 订单总条数
    allCount: 0,
    // 要显示的页数
    page: 1,
    // 每页要显示的条数
    size: 8,
    // 当前显示的数据类型
    showTeamType: 0,
    hasOrder: 0,
    // 是否显示更多数据
    showTips: 0,
    status: {},
    // 页面高度
    sysHeight: 0,
    // 控制加载
    loading: 0,
  },
  // 跳转至订单详情
  toOrderDetails: function (e) {
    let orderId = e.currentTarget.dataset.id;
    wx.setStorageSync('orderId', orderId)
    wx.navigateTo({
      url: '/pages/ucenter/order-details/index',
    })
  },
  // 点击订单支付
  payOrder: function (e) {
    let that = this;
    let orderId = e.currentTarget.dataset.orderid;
    let index = e.currentTarget.dataset.index
    let payOrderList = that.data.orderList[index].productList
    let userID = that.data.orderList[index].userID;
    wx.showModal({
      title: '提示',
      content: '确认支付该订单',
      success(res) {
        if (res.confirm) {
          util.requestApi(api.teamPlaceOrder, {
            userID: userID,
            teamOrderID: orderId,
            productList: payOrderList
          }).then(function (res) {
            if (res.code == 200) {
              let con = res.content
              wx.requestPayment({
                timeStamp: con.timeStamp,
                nonceStr: con.nonceStr,
                package: con.package,
                signType: con.signType,
                paySign: con.paySign,
                success(res) {
                  wx.showToast({
                    title: '下单成功',
                    icon: 'success',
                    duration: 2000
                  })
                  wx.navigateBack()
                },
                fail(res) {
                  wx.showToast({
                    title: '下单失败',
                    icon: 'none',
                    duration: 2000
                  })
                }
              })
            } else {
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              })
            }
          })
        } else if (res.cancel) {
          wx.showToast({
            title: '取消下单',
            icon: 'success',
            duration: 2000
          })
        }
      }
    })
  },
  // 获取订单详情
  // getOrderInfo: function (e) {
  //     let that = this;
  //     util.request(api.OrderCountInfo).then(function (res) {
  //         if (res.errno === 0) {
  //             let status = res.data;
  //             that.setData({
  //                 status: status
  //             });
  //         }
  //     });
  // },

  // 获取团购订单列表
  getUserTeamOrder(status) {
    let that = this
    let userID = wx.getStorageSync('userID')
    util.requestApi(api.getUserTeamOrder, {
      userID: userID,
      page: that.data.page,
      size: that.data.size,
      status: parseInt(status),
    }).then(function (res) {
      if (res.code == 200) {
        if (res.content.data.length > 0) {
          let list = res.content.data;
          for (let j = 0; j < list.length; j++) {
            let pList = res.content.data[j].productList;
            list[j].goodsCount = 0;
            list[j].goodsMoney = 0;
            for (let i = 0; i < pList.length; i++) {
              list[j].goodsCount += pList[i].count;
              list[j].goodsMoney += pList[i].count * pList[i].money
            }
          }
          that.setData({
            allCount: res.content.count,
            allPage: Math.ceil(res.content.count / 8),
            orderList: that.data.orderList.concat(list),
            showTips:list.length<that.data.size? 1:0,
            loading: 1
          });
        } else {
          that.setData({
            hasOrder: 1,
            loading: 1
          });
          wx.showToast({
            title: '无相关订单，快去下单吧',
            icon:'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 跳转首页
  toIndexPage: function (e) {
    wx.switchTab({
      url: '/pages/index/index'
    });
  },
  onLoad: function () {
    // 获取设备机型信息
    let phone = wx.getStorageSync('isIphoneX')
    if (phone) {
      this.setData({
        isIphoneX: 1
      })
    }
  },
  // 进入该界面 获取该界面的tab值 获取订单列表
  onShow: function () {
    // 获取设备信息
    let info = wx.getSystemInfoSync();
    let sysHeight = info.windowHeight - 100;
    this.setData({
      sysHeight: sysHeight,
    });
    // 要获取的团队定单的类型
    let showTeamType = 0;
    this.setData({
      showTeamType: showTeamType,
      orderList: [],
      allOrderList: [],
      allPage: 1,
      allCount: 0,
      size: 8
    });
    this.getUserTeamOrder(showTeamType)
  },
  // 切换tab
  switchTab: function (event) {
    let showTeamType = event.currentTarget.dataset.index;
    wx.setStorageSync('showTeamType', showTeamType);
    this.setData({
      showTeamType: showTeamType,
      orderList: [],
      allOrderList: [],
      page: 1,
      size: 8
    });
    this.getUserTeamOrder(showTeamType);
  },
  // “取消订单”点击效果
  cancelOrder: function (e) {
    let that = this;
    let orderId = e.currentTarget.dataset.index;
    wx.showModal({
      title: '',
      content: '确定要取消此订单？',
      success: function (res) {
        if (res.confirm) {
          util.request(api.OrderCancel, {
            orderId: orderId
          }, 'POST').then(function (res) {
            if (res.errno === 0) {
              wx.showToast({
                title: '取消订单成功'
              });
              that.setData({
                orderList: [],
                allOrderList: [],
                allPage: 1,
                allCount: 0,
                size: 8
              });
              that.getOrderList();
            } else {
              util.showErrorToast(res.errmsg);
            }
          });
        }
      }
    });
  },
  // 重置参数
  setParam: function () {
    this.setData({
      orderList: [],
      allOrderList: [],
      allPage: 1,
      allCount: 0,
      page: 1,
      size: 8,
      showTips: 0,
      loading: 0
    })
  },
  // 控制订单数据是否存在更多
  onReachBottom: function () {
    let that = this;
    if (that.data.page >= that.data.allPage) {
      that.setData({
        showTips: 1
      });
      return false;
    }
    that.setData({
      page: that.data.page + 1
    });
    that.getUserTeamOrder(that.data.showTeamType);
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.setParam();
    this.getUserTeamOrder(this.data.showTeamType);
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
})